package cn.range.filerecvserver;

import java.io.*;
import java.sql.SQLException;

import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.w3c.dom.Document;

import cn.range.utils.FileClassification;
import cn.range.utils.XMLDocumentInit;
import cn.range.domain.DefinedConstants;
import cn.range.utils.StreamTool;

/**
 * Servlet implementation class FileRecvServlet
 */
@WebServlet("/FileRecvServlet")
public class FileRecvServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileRecvServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*Quote from others Apr 4, 2013*/
		RequestContext req = new ServletRequestContext(request);
		if(FileUpload.isMultipartContent(req)){
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload fileUpload = new ServletFileUpload(factory);
			fileUpload.setFileSizeMax(2*1024);
			
			List items = new ArrayList();
			try {
				items = fileUpload.parseRequest(request);
			} catch (FileUploadException e) {
				e.printStackTrace();
			}
			
			Iterator it = items.iterator();
			while(it.hasNext()){
				FileItem fileItem = (FileItem) it.next();
				if(fileItem.isFormField()){
					System.out.println(fileItem.getFieldName() + "___" + fileItem.getName() + "___" +
							new String(fileItem.getString().getBytes("ISO-8859-1"), "GBK"));
				}
				else{
					System.out.println(fileItem.getFieldName() + "___" + fileItem.getName() + "___" +
							fileItem.isInMemory() + "___" + fileItem.getContentType() + "___" + 
							fileItem.getSize());
					if(fileItem.getName() != null && fileItem.getSize() != 0){
						File fullFile = new File(fileItem.getName());
						File newFile = new File("F:" + fullFile.getName());
						try {
							fileItem.write(newFile);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					else{
						System.out.println("No file chosen or empty file");
					}
				}
			}
		}
		System.out.println("Quote succeed.");
		
		
		/*Init works Apr 4, 2013
		DefinedConstants dc = new DefinedConstants();//A class defining all constants
		FileClassification fc = new FileClassification();//A class to classify and identify classtype
										//(with a poor name)
		XMLDocumentInit xmlInit = new XMLDocumentInit();//Class to init document xml
		//InputStream inStream = request.getInputStream();//Get input stream from request
		FileInputStream inStream = new FileInputStream("E:\\xmldata3.xml");
		
		
		try {
			Class.forName(dc.getDbDriverName());
			Document doc = xmlInit.initDoc(inStream);
			if(doc == null){
				System.out.println("Document init failed.");
			}
			else{
				fc.classFile(doc, dc);
				System.out.println("__________Over__________");
			}
		} catch (ClassNotFoundException e) {
			System.out.println("Can't Find JDBC Driver.");
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			inStream.close();
		}*/
		
		
		/*Several days ago, to recv xml*/
		/*try {
			byte[] data = StreamTool.read(request.getInputStream());
			String xml = new String(data, "UTF-8");
			System.out.println(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}

}
